//
//  MSAreaCell.m
//  SocietySale
//
//  Created by Mike on 16/11/4.
//  Copyright © 2016年 Mike. All rights reserved.
//

#define BtnWidth    (SCREEN_WIDTH-76-40)/3.0

#import "MSAreaCell.h"

@interface MSAreaCell ()
@property (nonatomic, strong) GXButton       *headBtn;       //
@property (nonatomic, strong) NSMutableArray *titleArray;    //标题
@property (nonatomic, strong) UIView         *bottomView;
@end

@implementation MSAreaCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
        self.contentView.backgroundColor = kDefaultViewBackgroundColor;
    }
    return self;
}

- (void)initView {
    _headBtn = [GXButton buttonWithType:UIButtonTypeCustom];
    _headBtn.frame = CGRectMake(0, 0, 76, 30);
    _headBtn.titleLabel.font = HeitiFont(14);
    [_headBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_headBtn setImage:[UIImage imageNamed:@"singleselect"] forState:UIControlStateNormal];
    [_headBtn setImage:[UIImage imageNamed:@"singleselected"] forState:UIControlStateSelected];
    [_headBtn addTarget:self action:@selector(headClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_headBtn];
    
    _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 15)];
    _bottomView.backgroundColor = kDefaultViewBackgroundColor;
    [self.contentView addSubview:_bottomView];
}

- (void)setDict:(NSDictionary *)dict {
    _dict = dict;
    NSString *key = [[dict allKeys] objectAtIndex:0];
    _titleArray = [dict objectForKey:key];
    for (int i=0; i<_stateArray.count; i++) {
        UIButton *btn = [self.contentView viewWithTag:100+i];
        if (!btn) {
            btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(76+10+(BtnWidth+10)*(i%3), i/3*(30+10), BtnWidth, 30);
            btn.tag = 100+i;
            btn.titleLabel.font = HeitiFont(14);
            btn.layer.cornerRadius = 2;
            btn.layer.borderWidth = 1;
            [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
            [self.contentView addSubview:btn];
        }
        NSString *stateStr = _stateArray[i];
        if ([stateStr isEqualToString:@"YES"]) {
            btn.selected = YES;
            [btn setTitleColor:kBlueColor forState:UIControlStateNormal];
            btn.layer.borderColor = kBlueColor.CGColor;
        }
        else{
            btn.selected = NO;
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            btn.layer.borderColor = kNormalGray.CGColor;
        }
        [btn setTitle:_titleArray[i] forState:UIControlStateNormal];
    }
    for (UIButton *btn in self.contentView.subviews) {
        if (btn.tag>=100+_titleArray.count) {
            [btn removeFromSuperview];
        }
    }
    if (_titleArray.count>3) {
        _bottomView.top = 60;
    }
    else{
        _bottomView.top = 30;
    }
    [self setHeadBtn];
    [_headBtn setTitle:key forState:UIControlStateNormal];
}

- (void)setStateArray:(NSMutableArray *)stateArray{
    _stateArray = stateArray;
}

- (void)headClick:(GXButton*)btn{
    btn.selected = !btn.selected;
    if (btn.selected) {
        for (int i=0; i<_stateArray.count; i++) {
            UIButton *btn = [self.contentView viewWithTag:100+i];
            btn.selected = YES;
            [btn setTitleColor:kBlueColor forState:UIControlStateNormal];
            btn.layer.borderColor = kBlueColor.CGColor;
            [_stateArray replaceObjectAtIndex:i withObject:@"YES"];
        }
    }
    else{
        for (int i=0; i<_stateArray.count; i++) {
            UIButton *btn = [self.contentView viewWithTag:100+i];
            btn.selected = NO;
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            btn.layer.borderColor = kNormalGray.CGColor;
            [_stateArray replaceObjectAtIndex:i withObject:@"NO"];
        }
    }
}

- (void)click:(UIButton*)btn{
    btn.selected = !btn.selected;
    NSInteger btnIndex = btn.tag - 100;
    if (btn.selected) {
        [btn setTitleColor:kBlueColor forState:UIControlStateNormal];
        btn.layer.borderColor = kBlueColor.CGColor;
        [_stateArray replaceObjectAtIndex:btnIndex withObject:@"YES"];
    }
    else{
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.layer.borderColor = kNormalGray.CGColor;
        [_stateArray replaceObjectAtIndex:btnIndex withObject:@"NO"];
    }
    [self setHeadBtn];
}

- (void)setHeadBtn{
    if (![_stateArray containsObject:@"NO"]) {
        _headBtn.selected = YES;
    }
    else{
        _headBtn.selected = NO;
    }
}

@end
